<template>
	<view class="u-wrap">
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in daylist" :key="index" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichDay(index,item)">
					<text class="u-line-1">{{item}}</text>
				</view>
			</scroll-view>
			<scroll-view scroll-y class="right-box">
				<view class="page-view">
					<view class="class-item">
						<view class="item-container">
							<view class="thumb-box" :class="{'on':item.choose,'not':item.disable}" v-for="(item, index) in time" :key="index" @tap.stop="swichTime(item)">
								<text class="item-menu-name">{{item.value}}</text>
								<!-- <text class="mark" v-if="item.choose"><u-icon class="icon" name="checkbox-mark" :color="'#2ab6b3'" size="14"></u-icon></text> -->
							</view>
						</view>
						<!-- <view class="u-btn bg-gradual-blue u-m-t-10 u-m-b-10">确定</view> -->
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// import classifyData from "@/common/classify.data.js";
	export default {
		props: {
			
		},
		data() {
			return {
				daylist: [
					
				],
				time: [
					{
						value: '尽快上门',
						choose: true
					},
					{
						value: '09:00-10:00',
						choose: false,
						disable: false
					},
					{
						value: '10:00-11:00',
						choose: false,
						disable: false
					},
					{
						value: '11:00-12:00',
						choose: false,
						disable: false
					},
					{
						value: '12:00-13:00',
						choose: false,
						disable: false
					},
					{
						value: '14:00-15:00',
						choose: false,
						disable: false
					},
					{
						value: '15:00-16:00',
						choose: false,
						disable: false
					},
					{
						value: '16:00-17:00',
						choose: false,
						disable: false
					},
					{
						value: '17:00-18:00',
						choose: false,
						disable: false
					},
					// {
					// 	value: '18:00-19:00',
					// 	choose: false,
					// 	disable: false
					// },
					// {
					// 	value: '19:00-20:00',
					// 	choose: false,
					// 	disable: false
					// }
				],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				date:{
					day: '',
					time: ''
				}
			}
		},
		created() {
			const date = new Date()
			const addDay = 10 //计算10天的日期，并push到daylist数组
			for(let i = 0;i < addDay;i++){
				let getDate = Date.parse(date) + (24 * 60 * 60 *1000 * i)
				this.daylist.push(this.$u.timeFormat(getDate, 'yyyy-mm-dd'))
			}
			this.date.day = this.daylist[0]
			
			this.getTime() // 判断当前时间，超过时间范围不可选
			
			// this.$set(this.date,'day',this.daylist[0])
			console.log(this.daylist)
		},
		methods: {
			getTime(){
				// 判断当前时间，超过时间范围不可选
				this.time.filter(arr=>arr.value != '尽快上门').forEach((item)=>{
					let now = new Date().getTime() + 2*60*60*1000 //表示当前时间后两个小时
					let time = new Date(this.date.day.replace(/-/g,'/') + ' ' + item.value.split('-')[1]).getTime()
					if(time < now){
						// uni.showToast({
						// 	icon: 'none',
						// 	title: '您不能选择过去的时间'
						// })
						item.disable = true
					}else{
						item.disable = false
					}
				})
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击日期切换
			async swichDay(index,item) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
				this.date.day = item;
				await this.getTime()
			},
			// 点击时间段
			swichTime(item){
				console.log(item)
				if(item.disable){
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '您不能选择过去的时间'
					// })
					return
				}
				this.time.forEach((item)=>{
					item.choose = false
				})
				item.choose = true
				this.date.time = item.value;
				if(item.value == '尽快上门'){
					this.$emit('swichday',{
						timetype: '尽快上门',
						day: this.date.day,
						time: this.date.time,
						show: false
					})
				}else{
					this.$emit('swichday',{
						timetype: '指定时间',
						day: this.date.day,
						time: this.date.time,
						show: false
					})
				}
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(500rpx);
		/* #ifdef H5 */
		// height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 260rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid $u-type-primary;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		flex: 1;
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		// padding: 16rpx;
	}

	.class-item {
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		// color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 208rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		// margin-top: 20rpx;
		margin: 20rpx 10rpx;
		border: 1px solid #e4e7ed;
		line-height: 60rpx;
		font-size: 24rpx;
		border-radius: 12rpx;
		position: relative;
		overflow: hidden;
		flex-flow: wrap;
		.mark{
			width: 28rpx;
			height: 28rpx;
			// background-color: #2ab6b3;
			// border-radius: 50%;
			// position: absolute;
			// right: 44rpx;
			// bottom: 60rpx;
			color: $u-color-main;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		&.on{
			color: $u-color-main;
			border-color: $u-color-main;
		}
		&.not{
			background-color: rgb(250, 250, 250);
			color: $u-tips-color;
		}
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
	
	// 模拟按钮
	.u-btn{
		font-size: 30rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 10rpx;
	}
</style>
